package com.meixiang.activity.account.manager;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Toast;

import com.meixiang.R;
import com.meixiang.fragment.account.BindPhoneFragment;
import com.meixiang.fragment.account.ConfirmPhoneFragment;
import com.meixiang.fragment.account.IBindPhoneClickListener;
import com.meixiang.fragment.account.VerifyIdentityFragment;
import com.meixiang.fragment.account.VerifyPhoneFragment;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.view.TitleView;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 绑定手机
 *
 * @author LZRUI
 */
public class BindPhoneActivity extends BaseActivity implements IBindPhoneClickListener {

    public static final String TAG = "BindPhoneActivity";

    private FragmentManager fragmentManager;

    /**
     * 手机是否能够接收信息选择页面
     */
    private BindPhoneFragment bindPhoneFragment;
    /**
     * 验证手机号码页面
     */
    private VerifyPhoneFragment verifyPhoneFragment;
    /**
     * 新手机号码绑定页面
     */
    private ConfirmPhoneFragment confirmPhoneFragment;
    /**
     * 身份验证页面（支付密码验证页面）
     */
    private VerifyIdentityFragment verifyIdentityFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_container);
        super.onCreate(savedInstanceState);
        initFragment();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HttpUtils.tagCancelRequest(TAG);
    }

    private void initFragment() {
        fragmentManager = getSupportFragmentManager();
        bindPhoneFragment = (BindPhoneFragment) fragmentManager.findFragmentById(R.id.fl_container);
        if (bindPhoneFragment == null) {
            bindPhoneFragment = BindPhoneFragment.newInstance();
            fragmentManager.beginTransaction().add(R.id.fl_container, bindPhoneFragment).commit();
        }

    }

    @Override
    protected void initTitle() {
        titles = (TitleView) findViewById(R.id.title);
        titles.setTitle("绑定手机");
        titles.setLeftImageButton(R.mipmap.back);
        titles.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回按钮监听事件
                switch (fragmentManager.getBackStackEntryCount()) {
                    case 0:
                        finish();
                        break;
                    case 1:
                        titles.setTitle("绑定手机");
                        break;
                }
                fragmentManager.popBackStack("back", FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void onBindPhoneFragmentClick(boolean canReceiveMsg) {
        if (canReceiveMsg) {
            // 能接收信息则进入短信验证页面
            getVerifyCode(false);
        } else {
            // 不能则进入支付密码验证页面
            if (verifyIdentityFragment == null) {
                verifyIdentityFragment = VerifyIdentityFragment.newInstance();
            }
            FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.detach(bindPhoneFragment);
            ft.add(R.id.fl_container, verifyIdentityFragment).addToBackStack("back").commit();
            titles.setTitle("身份验证");
        }
    }

    @Override
    public void onVerifyIdentityFragmentClick() {
        // 支付密码验证通过，进入新号码绑定界面
        confirmPhoneFragment = ConfirmPhoneFragment.newInstance(true);
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.detach(verifyIdentityFragment);
        ft.add(R.id.fl_container, confirmPhoneFragment).addToBackStack(null).commit();
        titles.setTitle("绑定手机");
    }

    @Override
    public void onVerifyPhoneFragmentClick() {
        // 进入新手机号码绑定界面
        confirmPhoneFragment = ConfirmPhoneFragment.newInstance(false);
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.detach(verifyPhoneFragment);
        ft.add(R.id.fl_container, confirmPhoneFragment).addToBackStack(null).commit();
        titles.setTitle("绑定手机");
    }

    @Override
    public void onConfirmPhoneFragmentClick() {
        // 绑定成功，直接退出页面
        finish();
    }

    /**
     * 获取验证码
     *
     * @param isReGetCode 是否是重新获取
     */
    public void getVerifyCode(final boolean isReGetCode) {
        HttpUtils.post(Config.SEND_RESET_MOBILE_SMS, TAG, null, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                Toast.makeText(BindPhoneActivity.this, message, Toast.LENGTH_LONG).show();
                if (!isReGetCode) {
                    if (verifyPhoneFragment == null) {
                        try {
                            verifyPhoneFragment = VerifyPhoneFragment.newInstance(result.getString("mobileSub4"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    FragmentTransaction ft = fragmentManager.beginTransaction();
                    ft.detach(bindPhoneFragment);
                    ft.add(R.id.fl_container, verifyPhoneFragment).addToBackStack("back").commit();
                    titles.setTitle("验证手机");
                }
            }

            @Override
            public void onError(String code, String message) {
                Toast.makeText(BindPhoneActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
